$(function() {
/*	if($("body").height()<$(window).height()-185){
		$("body").height($(window).height()-185);
	}*/
});
//全部
var roles=[];
//观看者
var viewerList=[];
//评论者
var commenterList=[];
//编辑者
var editorList=[];
//管理者
var adminList=[];
//创建者
var createrList=[];
//管理者和创建者特有的权限
var specificList=[];
//sharesavedownload对应的权限
var sharesavedownloads=[];
//peoples对应的权限
var peoples=[];
//invite对应的权限
var invites=[];
//post对应的权限
var posts=[];
//评论者的权限
var comarr = [];
//编辑者的权限
var edarr=[];
//全部
$.ajax({
	url:"/hivelearn/grouprole/findResourceList.json",
	data:{id:"group.editor"},
	success:function(data){
		var rs = data.data.resourceList;
		//去除提及组，提及成员,统计
		for(var i=0;i<rs.length;i++){
			if(rs[i].resourceCode!='group.mention'
				&&rs[i].resourceCode!='group.mentionGroup'
					&&rs[i].resourceCode!='group.analytics'){
				roles.push(rs[i]);
			}
		}
		createrList=roles;
		specificList=data.data.specificList;
		sharesavedownloads = data.data.sharesavedownload;
		peoples = data.data.people;
		invites = data.data.invite;
		posts = data.data.post;
		for(var i=0;i<roles.length;i++){
			var d = roles[i];
			if(specificList.indexOf(d.resourceCode)==-1){
				editorList.push(d);
				adminList.push(d);
			}else if(d.resourceCode=='people.role'){
				adminList.push(d);
			}
		}
	}
});
//观看者
$.ajax({
	url:"/hivelearn/grouprole/findResourceList.json",
	data:{id:"group.viewer"},
	success:function(data){
		viewerList = data.data.resourceList;
	}
});
//评论者
$.ajax({
	url:"/hivelearn/grouprole/findResourceList.json",
	data:{id:"group.commenter"},
	success:function(data){
		var rs = data.data.resourceList;
		//去除提及组，提及成员
		for(var i=0;i<rs.length;i++){
			if(rs[i].resourceCode!='group.mention'
				&&rs[i].resourceCode!='group.mentionGroup'
					&&rs[i].resourceCode!='group.analytics'){
				commenterList.push(rs[i]);
			}
		}
	}
});
/*var roles = ['group.update','group.setting','group.delete','topic.view','card.view',
             'post.view','card.like','post.like','card.comment','post.comment','card.share',
             'topic.share','topic.save','card.save','group.download','people.view',
             'post.createpost','people.invite','group.mention','group.mentionGroup',
             'group.analytics','topic.create','card.create','topic.edit','card.edit',
             'topic.delete','card.delete','post.delete','group.move'];
var role_group_update =new Object();
role_group_update.code = "group.update";
role_group_update.name = "修改群组";
var role_group_setting =new Object();
role_group_setting.code = "group.setting";
role_group_setting.name = "修改群组设置";
var role_group_delete =new Object();
role_group_delete.code = "group.delete";
role_group_delete.name = "删除群组";
var role_topic_view =new Object();
role_topic_view.code = "topic.view";
role_topic_view.name = "查看";
var role_card_view =new Object();
role_card_view.code = "card.view";
role_card_view.name = "";
var role_post_view =new Object();
role_post_view.code = "post.view";
role_post_view.name = "";
var role_card_like =new Object();
role_card_like.code = "card.like";
role_card_like.name = "点赞";
var role_post_like =new Object();
role_post_like.code = "post.like";
role_post_like.name = "";
var role_card_comment =new Object();
role_card_comment.code = "card.comment";
role_card_comment.name = "评论";
var role_post_comment =new Object();
role_post_comment.code = "post.comment";
role_post_comment.name = "";
var role_card_share =new Object();
role_card_share.code = "card.share";
role_card_share.name = "分享";
var role_topic_share =new Object();
role_topic_share.code = "topic.share";
role_topic_share.name = "";
var role_topic_save =new Object();
role_topic_save.code = "topic.save";
role_topic_save.name = "复制";
var role_card_save =new Object();
role_card_save.code = "card.save";
role_card_save.name = "";
var role_group_download =new Object();
role_group_download.code = "group.download";
role_group_download.name = "";
var role_people_view =new Object();
role_people_view.code = "people.view";
role_people_view.name = "查看成员";
var role_post_createpost =new Object();
role_post_createpost.code = "post.createpost";
role_post_createpost.name = "创建帖子";
var role_people_invite =new Object();
role_people_invite.code = "people.invite";
role_people_invite.name = "邀请";
var role_group_mention =new Object();
role_group_mention.code = "group.mention";
role_group_mention.name = "";
var role_group_mentionGroup =new Object();
role_group_mentionGroup.code = "group.mentionGroup";
role_group_mentionGroup.name = "";
var role_group_analytics =new Object();
role_group_analytics.code = "group.analytics";
role_group_analytics.name = "";
var role_topic_create =new Object();
role_topic_create.code = "topic.create";
role_topic_create.name = "创建";
var role_card_create =new Object();
role_card_create.code = "card.create";
role_card_create.name = "";
var role_topic_edit =new Object();
role_topic_edit.code = "topic.edit";
role_topic_edit.name = "修改";
var role_card_edit =new Object();
role_card_edit.code = "card.edit";
role_card_edit.name = "";
var role_topic_delete =new Object();
role_topic_delete.code = "topic.delete";
role_topic_delete.name = "删除";
var role_card_delete =new Object();
role_card_delete.code = "card.delete";
role_card_delete.name = "";
var role_post_delete =new Object();
role_post_delete.code = "post.delete";
role_post_delete.name = "";
var role_group_move =new Object();
role_group_move.code = "group.move";
role_group_move.name = "移动";
var role_people_role =new Object();
role_people_role.code = "people.role";
role_people_role.name = "修改角色";
//全部
var roleList=[role_group_update,role_group_setting,role_group_delete,role_topic_view,role_card_view,role_post_view,role_card_like,role_post_like,role_card_comment,
              role_post_comment,role_card_share,role_topic_share,role_topic_save,role_card_save,role_group_download,role_people_view,role_post_createpost,role_people_invite,
              role_group_mention,role_group_mentionGroup,role_group_analytics,role_topic_create,role_card_create,role_topic_edit,role_card_edit,role_topic_delete,role_card_delete,
              role_post_delete,role_group_move,role_people_role];
//viewer
var viewerList=[role_card_like,role_card_save,role_card_share,role_card_view,role_people_invite,
                role_post_createpost,role_post_like,role_post_view,role_topic_save,role_topic_share,role_topic_view];
//editor
var editorList=[role_card_like,role_card_save,role_card_share,role_card_view,role_people_invite,
                role_post_createpost,role_post_like,role_post_view,role_topic_save,role_topic_share,
                role_topic_view,role_card_comment,role_post_comment,role_topic_create,role_card_create,
                role_topic_edit,role_card_edit,role_topic_delete,role_card_delete,role_post_delete,role_group_move];
//commenter
var commenterList=[role_card_like,role_card_save,role_card_share,role_card_view,role_people_invite,
                   role_post_createpost,role_post_like,role_post_view,role_topic_save,role_topic_share,
                   role_topic_view,role_card_comment,role_post_comment];
//admin
var adminList=[role_card_like,role_card_save,role_card_share,role_card_view,role_people_invite,
               role_post_createpost,role_post_like,role_post_view,role_topic_save,role_topic_share,
               role_topic_view,role_card_comment,role_post_comment,role_topic_create,role_card_create,
               role_topic_edit,role_card_edit,role_topic_delete,role_card_delete,role_post_delete,
               role_group_move,role_people_role];
//creater
var createrList=[role_card_like,role_card_save,role_card_share,role_card_view,role_people_invite,
               role_post_createpost,role_post_like,role_post_view,role_topic_save,role_topic_share,
               role_topic_view, role_card_comment,role_post_comment,role_topic_create,role_card_create,
               role_topic_edit,role_card_edit,role_topic_delete,role_card_delete,role_post_delete,
               role_group_move,role_people_role,role_group_delete,role_group_setting,role_group_update];
*/

function isShow(fn) {
	$.ajax({
		url:'/hivelearn/grouprole/findRoleList.json',
		success:function(data){
			if(data.data&&data.data.length>0){
				for(var i = 0;i<roles.length;i++){
					if($("."+roles[i].resourceCode.replace(".",""))&&data.data.indexOf(roles[i].resourceCode)==-1){
						$("."+roles[i].resourceCode.replace(".","")).css("display","none");
					}
				}
			}
			hideColumn();
			if(fn){
				fn();
			}//回调函数
			//console.log(data);
		}
	})
}
//根据显隐，隐藏操作列
function hideColumn(){
	$(".dropdown-menu").each(function(){
		var flag = true;
		$(this).find("li").each(function(){
			if($(this).attr("class")!="divider"&&$(this).css("display")!="none"){
				flag =false;
				return;
			}
		});
		if(flag){
			$(this).closest(".hideColumn").hide();
			$(this).siblings(".hideColumn").hide();
		}else{
			$(this).closest(".hideColumn").show();
			$(this).siblings(".hideColumn").show();
		}
	})
}
//传入数组
function changeShow(arr){
	var str = "";
	for(var i=0;i<arr.length;i++){
		for(var j=0;j<roles.length;j++){
			if(arr[i]==roles[j].resourceCode&&roles[j].resourceName&&str.indexOf(roles[j].resourceName)==-1){
				str+=("，"+roles[j].resourceName);
			}
		}
	}
	return str.substring(1);
}
//获取中文名称
function nameShow(arr){
	var str = "";
	for(var i=0;i<arr.length;i++){
		if(arr[i].resourceName&&str.indexOf(arr[i].resourceName)==-1){
			str+=("，"+arr[i].resourceName);
		}
	}
	return str.substring(1);
}
//遮罩显示
function showloading(){
	$("#loadingModalId").modal({backdrop:'static',keyboard:false});
}
function hideloading(){
	$("#loadingModalId").modal('hide');
}
//根据群组获取评论者和编辑者的权限groupId为空时获得全部
function getGroupRole(groupId){
	if(!groupId){
		//构建评论者的权限
		for(var i=0;i<commenterList.length;i++){
			comarr.push(commenterList[i].resourceCode);
		}
		//构建编辑者的权限
		for(var i=0;i<editorList.length;i++){
			edarr.push(editorList[i].resourceCode);
		}
		$(".commenterRole").text(changeShow(comarr));
		$(".editorRole").text(changeShow(edarr));
	}else{
		$.ajax({
			url:"../../../hivelearn/group/findAboutGroup.json",
			data:{id:groupId},
			success:function(data){
				var d = data.data.roles;
				comarr=d.commenter;
				edarr=d.editor;
				$(".commenterRole").text(changeShow(comarr));
				$(".editorRole").text(changeShow(edarr));
			}
		})
	}
}
//角色资源加载（编辑者，评论者）
function commmon_initRole(){
		var val = $("[name='sharesavedownload']").val();
		var peopleVal = $("[name='people']").val();
		var inviteVal = $("[name='invite']").val();
		var postsVal = $("[name='posts']").val();
		//所有可选权限
		var chooseRole =[];
		chooseRole = chooseRole.concat(sharesavedownloads);
		chooseRole = chooseRole.concat(peoples);
		chooseRole = chooseRole.concat(invites);
		chooseRole = chooseRole.concat(posts);
		//构建评论者的权限
		var dels = [];
		for(var i=0;i<comarr.length;i++){
			if(chooseRole.indexOf(comarr[i])!=-1){
				dels.push(i);
			}
		}
		for(var i=dels.length-1;i>=0;i--){
			comarr.splice(dels[i], 1);
		}
		//构建编辑者的权限
		var eddels=[];
		for(var i=0;i<edarr.length;i++){
			if(chooseRole.indexOf(edarr[i])!=-1){
				eddels.push(i);
			}
		}
		for(var i=eddels.length-1;i>=0;i--){
			edarr.splice(eddels[i], 1);
		}
		
		if(val==4||val==3){
			edarr = edarr.concat(sharesavedownloads);
		}
		if(val==4){
			comarr = comarr.concat(sharesavedownloads);
		}
		
		if(peopleVal==4||peopleVal==3){
			edarr = edarr.concat(peoples);
		}
		if(peopleVal==4){
			comarr = comarr.concat(peoples);
		}
		
		if(inviteVal==4||inviteVal==3){
			edarr = edarr.concat(invites);
		}
		if(inviteVal==4){
			comarr = comarr.concat(invites);
		}
		
		if(postsVal==4||postsVal==3){
			edarr = edarr.concat(posts);
		}
		if(postsVal==4){
			comarr = comarr.concat(posts);
		}
		$(".commenterRole").text(changeShow(comarr));
		$(".editorRole").text(changeShow(edarr));
}
//角色资源加载（sharesavedownload范围，people范围,invite范围，posts范围）注：范围是（1、仅自己，2、自己和管理员，3、自己和管理员和编辑者，4、所有人）
function c_initRole(sharesavedownload,people,invite,post){
		//所有可选权限
		var chooseRole =[];
		chooseRole = chooseRole.concat(sharesavedownloads);
		chooseRole = chooseRole.concat(peoples);
		chooseRole = chooseRole.concat(invites);
		chooseRole = chooseRole.concat(posts);
		//构建评论者的权限
		var dels = [];
		var com_arr=[];
		for(var i=0;i<commenterList.length;i++){
			com_arr.push(commenterList[i].resourceCode);
		}
		for(var i=0;i<com_arr.length;i++){
			if(chooseRole.indexOf(com_arr[i])!=-1){
				dels.push(i);
			}
		}
		for(var i=dels.length-1;i>=0;i--){
			com_arr.splice(dels[i], 1);
		}
		//构建编辑者的权限
		var eddels=[];
		var ed_arr=[];
		for(var i=0;i<editorList.length;i++){
			ed_arr.push(editorList[i].resourceCode);
		}
		for(var i=0;i<ed_arr.length;i++){
			if(chooseRole.indexOf(ed_arr[i])!=-1){
				eddels.push(i);
			}
		}
		for(var i=eddels.length-1;i>=0;i--){
			ed_arr.splice(eddels[i], 1);
		}
		//构建观看者的权限
		var videls=[];
		var vi_arr=[];
		for(var i=0;i<viewerList.length;i++){
			vi_arr.push(viewerList[i].resourceCode);
		}
		for(var i=0;i<vi_arr.length;i++){
			if(chooseRole.indexOf(vi_arr[i])!=-1){
				videls.push(i);
			}
		}
		for(var i=videls.length-1;i>=0;i--){
			vi_arr.splice(videls[i], 1);
		}
		//构建管理者的权限
		var addels=[];
		var ad_arr=[];
		for(var i=0;i<adminList.length;i++){
			ad_arr.push(adminList[i].resourceCode);
		}
		for(var i=0;i<ad_arr.length;i++){
			if(chooseRole.indexOf(ad_arr[i])!=-1){
				addels.push(i);
			}
		}
		for(var i=addels.length-1;i>=0;i--){
			ad_arr.splice(addels[i], 1);
		}
		
		//sharesavedownloads
		if(sharesavedownload!=1){
			ad_arr = ad_arr.concat(sharesavedownloads);
		}
		if(sharesavedownload==4||sharesavedownload==3){
			ed_arr = ed_arr.concat(sharesavedownloads);
		}
		if(sharesavedownload==4){
			com_arr = com_arr.concat(sharesavedownloads);
			vi_arr = vi_arr.concat(sharesavedownloads);
		}
		//peoples
		if(people!=1){
			ad_arr = ad_arr.concat(peoples);
		}
		if(people==4||people==3){
			ed_arr = ed_arr.concat(peoples);
		}
		if(people==4){
			com_arr = com_arr.concat(peoples);
			vi_arr = vi_arr.concat(peoples);
		}
		//invites
		if(invite!=1){
			ad_arr = ad_arr.concat(invites);
		}
		if(invite==4||invite==3){
			ed_arr = ed_arr.concat(invites);
		}
		if(invite==4){
			com_arr = com_arr.concat(invites);
			vi_arr = vi_arr.concat(invites);
		}
		//post
		if(post!=1){
			ad_arr = ad_arr.concat(posts);
		}
		if(post==4||post==3){
			ed_arr = ed_arr.concat(posts);
		}
		if(post==4){
			com_arr = com_arr.concat(posts);
			vi_arr = vi_arr.concat(posts);
		}
		$(".c_commenter").text(changeShow(com_arr));
		$(".c_editor").text(changeShow(ed_arr));
		$(".c_viewer").text(changeShow(vi_arr));
		$(".c_admin").text(changeShow(ad_arr));
}

//ie下target:当前input,form:表单id,fn:上传函数,filemaxsize:最大值
function ieUpload(target,form,fn,filemaxsize){
	$("#"+form).ajaxSubmit({
  	url: "../../../hivelearn/topic/getFileSize.json?_t="+new Date().getTime(),
      data:$("#"+form).serialize(),
      dataType:"json",
      success:function(data){
			fileSize=data;
			var size = fileSize / 1024;
			if (size > filemaxsize) {
				$("#msgModal").find(".msg").text("图片大小不能大于" + filemaxsize / 1024 + "M！");
				$("#msgModal").modal("show");
				target.value = "";
				return false;
			}
			if (size <= 0) {
				$("#msgModal").find(".msg").text("图片大小不能为0M！");
				$("#msgModal").modal("show");
				target.value = "";
				return false;
			}
			fn();
		},
		error:function(data){
			$("#msgModal").find(".msg").text("图片不存在！");
			$("#msgModal").modal("show");
			target.value = "";
		}
  });
}
//检测
function commoncheckfile(target,formId,fn,types,maxsize) {
	if(!maxsize){
		maxsize = 2;//默认2m
	}
	if(!types){
		types=".jpg,.jpeg,.png,.gif";
	}
	var isIE = /msie/i.test(navigator.userAgent) && !window.opera; 
	var fileSize = 0;
	var filetypes = types.split(",");
	var filepath = target.value;
	var filemaxsize = 1024 * maxsize;// 2M
	if (filepath) {
		var isnext = false;
		var fileend = filepath.substring(filepath.lastIndexOf("."));
		if (filetypes && filetypes.length > 0) {
			for ( var i = 0; i < filetypes.length; i++) {
				if (filetypes[i] == fileend) {
					isnext = true;
					break;
				}
			}
		}
		if (!isnext) {
			$("#msgModal").find(".msg").text("请选择"+types+"文件上传");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
	} else {
		return false;
	}
	if (isIE && !target.files) {
		ieUpload(target,formId,fn,filemaxsize);
	} else {
		fileSize = target.files[0].size;
		var size = fileSize / 1024;
		if (size > filemaxsize) {
			$("#msgModal").find(".msg").text("图片大小不能大于" + filemaxsize / 1024 + "M！");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
		if (size <= 0) {
			$("#msgModal").find(".msg").text("图片大小不能为0M！");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
		fn();
	}
}